package com.nettyAdvanced.p1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author： pang-yun
 * @date： 2021-09-14 14:25
 * 功能目的 ：用于  观察  黏包现象的 服务端
 */

public class HelloWorldServer {

    public static void main(String[] args) throws InterruptedException {
        start();
    }


    static void start() throws InterruptedException {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup work = new NioEventLoopGroup();

        ChannelFuture channelFuture = new ServerBootstrap()
                .group(boss, work)
                .channel(NioServerSocketChannel.class)
                //为观察 半包现象  将 ServerSocketChannel 的缓冲区 调到10
                .option(ChannelOption.SO_RCVBUF,8)
                .childHandler(
                        new ChannelInitializer<NioSocketChannel>() {
                            @Override
                            protected void initChannel(NioSocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                            }
                        }
                ).bind(9999);

        Channel channel = channelFuture.sync().channel();
        ChannelFuture closeFuture = channel.closeFuture();
        closeFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                boss.shutdownGracefully();
                work.shutdownGracefully();
            }
        });
    }


}
